//
//  CABasicAnimation+ZJAnimation.m
//  YueLoveBaby
//
//  Created by 张敬 on 2018/4/21.
//  Copyright © 2018年 张敬. All rights reserved.
//

#import "CABasicAnimation+ZJAnimation.h"

@implementation CABasicAnimation (ZJAnimation)

+ (CABasicAnimation *)scaleXByParam:(float)param
{
    CABasicAnimation *scaleXAin = [CABasicAnimation animation];
    scaleXAin.keyPath = @"transform.scale.x";
    scaleXAin.fromValue = @1.0;
    scaleXAin.toValue = @(param);
    return scaleXAin;
}

//竖直方向移动
+ (CABasicAnimation *)moveYPositionWithFrom:(float)from to:(float)to
{
    return [self moveAnimationWithFrom:from to:to diction:@"position.y" isDelay:NO];
}

//水平方向移动
+ (CABasicAnimation *)moveXPositionWithFrom:(float)from to:(float)to
{
    return [self moveAnimationWithFrom:from to:to diction:@"position.x" isDelay:NO];
}

+ (CABasicAnimation *)moveAnimationWithFrom:(float)from to:(float)to  diction:(NSString *)diction isDelay:(BOOL)isdelay
{
    CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:diction];
    move.fromValue = @(from);
    move.toValue = @(to);
    move.removedOnCompletion = NO;
    move.fillMode = kCAFillModeForwards;
    return move;
}

+ (void)transformScaleAnimationWithLayer:(CALayer *)layer duration:(float)duration fromValue:(float)fromValue toValue:(float)toValue autoreverses:(BOOL)autoreverses repeatCount:(float)repeatCount
{
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
    animation.fromValue = [NSNumber numberWithFloat:fromValue];
    animation.toValue = [NSNumber numberWithFloat:toValue];
    animation.duration = duration;
    animation.autoreverses = autoreverses;            //结束后是否逆转播放
    animation.fillMode = kCAFillModeForwards;
    if (repeatCount == 0) {
        animation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }else {
        animation.repeatCount = repeatCount; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }
    animation.removedOnCompletion = NO;     //切换页面或者按HOME键进入前台 再次进入app保持动画一直运行。
    [layer addAnimation:animation forKey:nil];
}


+ (void)transformRotationZAnimationWithLayer:(CALayer *)layer duration:(float)duration fromValue:(float)fromValue toValue:(float)toValue autoreverses:(BOOL)autoreverses repeatCount:(float)repeatCount
{
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
    animation.fromValue = [NSNumber numberWithFloat:fromValue];
    animation.toValue = [NSNumber numberWithFloat:toValue];
    animation.duration = duration;
    animation.autoreverses = autoreverses;            //结束后是否逆转播放
    animation.fillMode = kCAFillModeForwards;
    if (repeatCount == 0) {
        animation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }else {
        animation.repeatCount = repeatCount; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }
    animation.removedOnCompletion = NO;     //切换页面或者按HOME键进入前台 再次进入app保持动画一直运行。
    [layer addAnimation:animation forKey:nil];
}


+ (void)transformPositionAnimationWithLayer:(CALayer *)layer duration:(float)duration fromValue:(CGPoint)fromValue toValue:(CGPoint)toValue autoreverses:(BOOL)autoreverses repeatCount:(float)repeatCount
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [animation setFromValue:[NSValue valueWithCGPoint:fromValue]];
    [animation setToValue:[NSValue valueWithCGPoint:toValue]];
    [animation setAutoreverses:autoreverses];
    [animation setDuration:duration];
    if (repeatCount == 0) {
        animation.repeatCount = MAXFLOAT; //如果这里想设置成一直执行，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }else {
        animation.repeatCount = repeatCount; //如果这里想设置成一直执行，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }
    animation.removedOnCompletion = NO;     //切换页面或者按HOME键进入前台 再次进入app保持动画一直运行。
    [layer addAnimation:animation forKey:nil];
}


+ (void)transformPointsPositionAnimationWithLayer:(CALayer *)layer duration:(float)duration pointValueArray:(NSArray *)pointValueArray autoreverses:(BOOL)autoreverses repeatCount:(float)repeatCount isComplentBack:(BOOL)isComplentBack
{
    CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.duration = duration;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    animation.values = pointValueArray;
    animation.autoreverses = autoreverses;            //结束后是否逆转播放
    if (isComplentBack == YES) {
        animation.fillMode = kCAFillModeBackwards;
    }else {
        animation.fillMode = kCAFillModeForwards;
    }
    if (repeatCount == 0) {
        animation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }else {
        animation.repeatCount = repeatCount; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }
    animation.removedOnCompletion = NO;     //切换页面或者按HOME键进入前台 再次进入app保持动画一直运行。
    [layer addAnimation:animation forKey:nil];
}


+ (void)transformAnimationGroupWithLayer:(CALayer *)layer duration:(float)duration animationArray:(NSArray *)animationArray autoreverses:(BOOL)autoreverses repeatCount:(float)repeatCount isComplentBack:(BOOL)isComplentBack
{
    CAAnimationGroup * group = [CAAnimationGroup animation];
    group.animations = animationArray;
    group.duration = duration;
    group.autoreverses = autoreverses;            //结束后是否逆转播放
    if (isComplentBack == YES) {
        group.fillMode = kCAFillModeBackwards;
    }else {
        group.fillMode = kCAFillModeForwards;
    }
    group.removedOnCompletion = NO;
    if (repeatCount == 0) {
        group.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }else {
        group.repeatCount = repeatCount; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [layer addAnimation:group forKey:nil];
}


//MARK: - 快捷创建CABasicAnimation
+ (CABasicAnimation *)makeScaleRotationBasicAnimationWithKeyPath:(NSString *)keyPath duration:(float)duration fromValue:(float)fromValue toValue:(float)toValue autoreverses:(BOOL)autoreverses repeatCount:(float)repeatCount isComplentBack:(BOOL)isComplentBack
{
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:keyPath];
    //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
    animation.fromValue = [NSNumber numberWithFloat:fromValue];
    animation.toValue = [NSNumber numberWithFloat:toValue];
    animation.duration = duration;
    animation.autoreverses = autoreverses;            //结束后是否逆转播放
    if (isComplentBack == YES) {
        animation.fillMode = kCAFillModeBackwards;
    }else {
        animation.fillMode = kCAFillModeForwards;
    }
    if (repeatCount == 0) {
        animation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }else {
        animation.repeatCount = repeatCount; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }
    animation.removedOnCompletion = NO;     //切换页面或者按HOME键进入前台 再次进入app保持动画一直运行。
    return animation;
}

+ (CABasicAnimation *)makePositionBasicAnimationWithDuration:(float)duration fromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint autoreverses:(BOOL)autoreverses repeatCount:(float)repeatCount isComplentBack:(BOOL)isComplentBack
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    [animation setFromValue:[NSValue valueWithCGPoint:fromPoint]];
    [animation setToValue:[NSValue valueWithCGPoint:toPoint]];
    [animation setAutoreverses:autoreverses];
    [animation setDuration:duration];
    if (isComplentBack == YES) {
        animation.fillMode = kCAFillModeBackwards;
    }else {
        animation.fillMode = kCAFillModeForwards;
    }
    if (repeatCount == 0) {
        animation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }else {
        animation.repeatCount = repeatCount; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }
    animation.removedOnCompletion = NO;     //切换页面或者按HOME键进入前台 再次进入app保持动画一直运行。
    return animation;
}


+ (CAKeyframeAnimation *)makeKeyFramePositionKeyFrameAnimationWithDuration:(float)duration pointValueArray:(NSArray *)pointValueArray  autoreverses:(BOOL)autoreverses repeatCount:(float)repeatCount isComplentBack:(BOOL)isComplentBack
{
    CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.duration = duration;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    animation.values = pointValueArray;
    animation.autoreverses = autoreverses;            //结束后是否逆转播放
    if (isComplentBack == YES) {
        animation.fillMode = kCAFillModeBackwards;
    }else {
        animation.fillMode = kCAFillModeForwards;
    }
    if (repeatCount == 0) {
        animation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }else {
        animation.repeatCount = repeatCount; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    }
    animation.removedOnCompletion = NO;     //切换页面或者按HOME键进入前台 再次进入app保持动画一直运行。
    return animation;
}

+ (CABasicAnimation *)makeTransferAnimationByFromValue:(CGFloat)fromValue toValue:(CGFloat)toValue{
    
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
    animation.fromValue = [NSNumber numberWithFloat:fromValue];
    animation.toValue = [NSNumber numberWithFloat:toValue];
    animation.duration = 1.0;
    animation.fillMode = kCAFillModeForwards;
    animation.repeatCount = 1; //如果这里想设置成一直自旋转，可以设置为MAXFLOAT，否则设置具体的数值则代表执行多少次
    animation.removedOnCompletion = NO;     //切换页面或者按HOME键进入前台 再次进入app保持动画一直运行。

    return animation;
}

+ (CAKeyframeAnimation *)makeGravityJumpAnimation{
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
    //通过初中物理重力公式计算出的位移y值数组
    animation.values = @[@0.0, @-4.15, @-7.26, @-9.34, @-10.37, @-9.34, @-7.26, @-4.15, @0.0, @2.0, @-2.9, @-4.94, @-6.11, @-6.42, @-5.86, @-4.44, @-2.16, @0.0];
   animation.duration = 0.5;
    animation.beginTime = CACurrentMediaTime() + 1;
    
    return animation;
}

+ (CABasicAnimation *)makeScaleBigAndSamllAnimation{
    //放大效果，并回到原位
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    //速度控制函数，控制动画运行的节奏
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration = 0.2;       //执行时间
    animation.repeatCount = 1;      //执行次数
    animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
    animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
    animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
    return animation;
}

+ (CABasicAnimation *)makeAlpleAnimationByFromValue:(CGFloat)fromVale toValue:(CGFloat)toValue{
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    basicAnimation.fromValue = @(fromVale);
    basicAnimation.toValue = @(toValue);
    basicAnimation.duration = 1.5;
    
    return basicAnimation;
}

//- (void)addAnimateForView:(UIView *)view withRect:(CGRect)rect{
//    CALayer *layer = [CALayer layer];//创建一个layer，最后用来添加到view的图层上展示动画用
//    NSInteger repeatCount = 3;//设置重复次数3次
//    NSInteger keepTiming = 1.5;// 设置每段动画持续时间3秒
//    
//    for (NSInteger i = 0; i< repeatCount; i++) {//每次执行，创建相关动画
//        // 每个动画对应一个图层。3个动画，需要有3个图层
//        CALayer *animateLayer = [CALayer layer];
//        animateLayer.borderColor = mMainColor.CGColor;
//        animateLayer.borderWidth = 1.5;
//        CGFloat x = (view.width - rect.size.width)/2.0;
//        CGFloat y = (view.height - rect.size.height)/2.0;
//        animateLayer.frame = CGRectMake(x, y, rect.size.width, rect.size.height);
//        animateLayer.cornerRadius = rect.size.height/2;
//        //到此。每一个图层的大小，形状。颜色设置完毕。
//        // 设置图层的scale 使用CABasicAnimation
//        CABasicAnimation *basicAni = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//        basicAni.fromValue = @1.0f;
//        basicAni.toValue = @3.0f;
//        
//        //设置图层的透明度，使用关键帧动画
//        CAKeyframeAnimation *keyani = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
//        keyani.values = @[@1, @0.9, @0.8, @0.7, @0.6, @0.5, @0.4, @0.3, @0.2, @0.1, @0];
//        keyani.keyTimes = @[@0, @0.1, @0.2, @0.3, @0.4, @0.5, @0.6, @0.7, @0.8, @0.9, @1];
//        
//        //我们要让一个动画同时执行scale 和 opacity的变化，需要将他们都加入到layer的动画组
//        CAAnimationGroup *group = [CAAnimationGroup animation];
//        group.fillMode = kCAFillModeBackwards;
//        group.duration = keepTiming;
//        group.repeatCount = repeatCount;
//        group.beginTime = CACurrentMediaTime() + (double)i * keepTiming / (double)repeatCount;
//        
//        group.animations = @[keyani,basicAni];
//        [animateLayer addAnimation:group forKey:@"plus"];
//        [layer addSublayer:animateLayer];
//    }
//    [view.layer addSublayer:layer];
//}




@end
